﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NLog;

namespace KBVault.Web.Infrastructure
{
    public class KBVaultBasePage : System.Web.UI.Page
    {
        public Logger Log = LogManager.GetCurrentClassLogger();
        
        public KBVaultBasePage()
            : base()
        {
            Load += Page_Load;
        }

        protected void Page_Load(object o, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                if (Request.QueryString["Category"] != null)
                {
                    if (ActiveCategoryId < 0)
                    {
                        try
                        {
                            ActiveCategoryId = Convert.ToInt32(Request.QueryString["Category"]);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex.Message);
                            ActiveCategoryId = -1;
                        }
                    }
                }
                if (Request.QueryString["Article"] != null)
                {
                    if (ActiveArticleId < 0)
                    {
                        try
                        {
                            ActiveArticleId = Convert.ToInt32(Request.QueryString["Article"]);
                        }
                        catch (Exception ex)
                        {
                            Log.Error(ex.Message);
                            ActiveArticleId = -1;
                        }
                    }
                }
            }
        }
        public string CurrentUrl { get { return HttpUtility.UrlEncode(Request.Url.PathAndQuery); } }
        public string ReturnUrl { get { return HttpUtility.UrlDecode(Request.QueryString["ReturnUrl"]); } }
        public string ReturnUrlParameter
        {
            get
            {
                return "ReturnUrl="+CurrentUrl;
            }
        }

        public int ActiveCategoryId
        {
            get
            {
                return (ViewState["ActiveCategoryId_212"] != null) ? Convert.ToInt32(ViewState["ActiveCategoryId_212"]) : -1;
            }
            set
            {
                ViewState["ActiveCategoryId_212"] = value;
            }
        }

        public int ActiveArticleId
        {
            get
            {
                return (ViewState["ActiveArticleId_243"] != null) ? Convert.ToInt32(ViewState["ActiveArticleId_243"]) : -1;
            }
            set
            {
                ViewState["ActiveArticleId_243"] = value;
            }
        }

        public void ShowExceptionError()
        {
            ShowError("Application encountered an error and error has been logged.Please examine logs and load this page later.");
        }
        public void ShowError( string message)
        {
            (this.Master as Admin).ShowError(message);
        }
        
        public void ShowMessage(string message)
        {
            (this.Master as Admin).ShowMessage(message);
        }
        
    }
}